<!-- 商品分类管理 -->
<template>
	<section>
		<el-col :span="24" class="toolbar" style="display: flex;justify-content: space-between;margin-bottom: -10px;">
			<div>
				<p class="text">商品分类管理</p>
			</div>
			<el-form :inline="true" :model="filters">
				<el-form-item >
					<el-button type="primary" @click="dialogVisible = true" icon="el-icon-plus">添加</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="List" highlight-current-row v-loading="loading" @selection-change="selsChange" border :cell-style="cellStyle" :header-cell-style="rowClass" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column prop="id" align="center" label="ID" width="60">
			</el-table-column>
			<el-table-column align="center" label="分类图">
				<template slot-scope="scope">
					<img :src="scope.row.img" alt="" style="width: 30%;">
				</template>
			</el-table-column>
			<el-table-column prop="categoryName" align="center" label="分类名称">
			</el-table-column>
			<el-table-column prop="sortNo" align="center" label="排序号" >
			</el-table-column>
			<el-table-column prop="categoryStatus" align="center" label="状态" :formatter="formatSex"></el-table-column>
			<el-table-column prop="createTime" align="center" label="时间" >
			</el-table-column>
			<el-table-column label="操作" align="center" >
				<template slot-scope="scope">
					<el-button size="small" type="warning" icon="el-icon-edit" @click="handleEdit(scope.$index, scope.row)">修改</el-button>
				</template>
			</el-table-column>
		</el-table>
		<!--批量删除-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">单个/批量删除</el-button>
			<el-pagination background layout="total,sizes,prev, pager, next,jumper" @size-change="handleSizeChange" @current-change="handleCurrentChange" :page-size="5" :total="total" style="float:right;">
			</el-pagination>
		</el-col>
		<!--商品分类添加-->
		<el-dialog title="商品分类添加" :visible.sync="dialogVisible" width="50%" :before-close="handleClose">
			<el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
				<el-form-item label="分类名称" prop="categoryName">
					<el-input v-model="addForm.categoryName" auto-complete="off" placeholder="请输入分类名称"></el-input>
				</el-form-item>
				<el-form-item label="分类图片" prop="img">
					<el-upload class="upload-demo" 
						action="/manager/common/upload/uploadFile" 
						:on-preview="handlePreview" 
						:on-remove="handleRemove" 
						:on-success="handleSuccess" 
						ref="upload1" 
						:file-list="fileList" 
						:before-upload="beforeAvatarUpload"
						list-type="picture"
					>
					    <el-button size="small" type="primary">选择分类图</el-button>
					    <div slot="tip" class="el-upload__tip">请上传规格为80X80的分类图，大小不超过5kb</div>
					</el-upload>
				</el-form-item>
				<el-form-item label="排列序号" prop="sortNo">
					<el-select v-model="addForm.sortNo" placeholder="请选择排列序号,序号大于 0">
						<el-option v-for="item in sortNoList" :key="item.label" :label="item.label" :value="item.label"></el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="状态" prop="status">
					<el-radio-group v-model="addForm.categoryStatus">
						<el-radio class="radio" :label="1">可用</el-radio>
						<el-radio class="radio" :label="0">不可用</el-radio>
					</el-radio-group>
				</el-form-item>
			</el-form>
			<span slot="footer" class="dialog-footer">
				<el-button @click="dialogVisible = false">取 消</el-button>
				<el-button type="primary"  @click.native="addSubmit"  :loading="loading">确定</el-button>
			</span>
		</el-dialog>
		<!--商品分类修改-->
		<el-dialog title="商品分类修改" :visible.sync="editFormVisible" width="50%" :before-close="handleClose">
			<el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
				<el-form-item label="分类名称" prop="categoryName">
					<el-input v-model="editForm.categoryName" auto-complete="off" placeholder="请输入分类名称"></el-input>
				</el-form-item>
				<el-form-item label="分类图">
					<img :src="editForm.img" alt="" style="width:30%;">
				</el-form-item>
				<el-form-item label="排列序号" prop="sortNo">
					<el-select v-model="editForm.sortNo" placeholder="请选择排列序号,序号大于 0">
						<el-option v-for="item in sortNoList" :key="item.label" :label="item.label" :value="item.label"></el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="状态" prop="status">
					<el-radio-group v-model="editForm.categoryStatus">
						<el-radio class="radio" :label="1">可用</el-radio>
						<el-radio class="radio" :label="0">不可用</el-radio>
					</el-radio-group>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="loading">确定</el-button>
			</div>
		</el-dialog>
	</section>
</template>
<script>
	import util from '../../../common/js/util'
	import {
		getGoodsCategory,
		addGoodsCategory,
		editGoodsCategory,
		delGoodsCategory,
		Refreshtoken
	} from '../../../api/api';

	export default {
		data() {
			return {
				filters: {},
				loading: false,
				List: [],
				total: 0,
				page: 1,
				size:10,
				sels: [],//列表选中列
				timer: null,
				dialogVisible: false,
				addForm:{
					categoryName:'',
					sortNo:0,
					categoryStatus:1,
					img:''
				},
				addFormRules: {
					categoryName: [{
						required: true,
						message: '请输入名称',
						trigger: 'blur'
					}],
				},
				fileList: [],
				editFormVisible:false,
				editForm:{
					categoryName:'',
					sortNo:0,
					categoryStatus:1
				},
				editFormRules: {
					categoryName: [{
						required: true,
						message: '请输入名称',
						trigger: 'blur'
					}],
				},
				sortNoList:[
					{label:1},{label:2},{label:3},{label:4},{label:5},{label:6},
					{label:7},{label:8},{label:9},{label:10},{label:11},{label:12},
					{label:13},{label:14},{label:15},{label:16},{label:17},{label:18},
					{label:19},{label:20},{label:21},{label:22},{label:23},{label:24},
					{label:25},{label:26},{label:27},{label:28},{label:29},{label:30},
				]
			}
		},
		mounted() {
			var Token = sessionStorage.getItem('Token');
			var timestamp=new Date().getTime();
			let para = {
				token: Token,
				curpage:this.page,
				pagesize:this.size,
				timestamp: timestamp
			};
			this.loading = true;
			getGoodsCategory(para).then((res) => {
				var res = res.data;
				if (res.code != 0) {
					this.$message({
						message: res.message,
						type: 'error'
					});
				} else {
					this.total = res.data.total;
					this.List = res.data.list;
					this.loading = false;
				}
			});
			
			this.queryInfo();
			this.timer = setInterval(() => {
			   setTimeout(this.queryInfo, 0)
		    }, 600000)
		},
		methods: {
			// 设置定时器
			queryInfo() {
				var Token = sessionStorage.getItem('Token');
				var timestamp=new Date().getTime();
				let para = {token: Token,timestamp: timestamp};
				Refreshtoken(para).then((res) => {
					var res = res.data;
					if (res.code != 0) {
						this.$message({
							message: res.message,
							type: 'error'
						});
					} else {
						this.loading = false;
					}
				});
			},
			// 转换用户状态
			formatSex: function (row, column) {
				return row.categoryStatus == 1 ? '正常' : row.categoryStatus == 0 ? '禁用' : '未知';
			},
			selsChange: function (sels) {
				this.sels = sels;
			},
			// 设置表格样式
			cellStyle({row, column}){return 'text-align:center';},
			rowClass({row, column}){return 'text-align:center';},
			// 设置分页
			handleCurrentChange(val) {this.page = val;this.getCategory();},
			handleSizeChange(val) {this.size = val;this.getCategory();},
			// 图片上传
			handleRemove(file, fileList) {},
			handlePreview(file) {},
			handleSuccess(res,file) {this.imgUrl = file.response.data[0].url},
			beforeAvatarUpload(file) {
				const isJPG = file.type === 'image/png';
				const isLt2M = file.size / 1024 < 5;
				if (!isJPG) {
				  this.$message.error('上传图片只能是 png 格式!');
				}
				if (!isLt2M) {
				  this.$message.error('上传图片大小不能超过 5Kb!');
				}
				return isJPG && isLt2M;
			},
			// 添加商品分类
			handleClose(done) {
				this.$confirm('确认关闭？')
					.then(_ => {
						done();
					})
					.catch(_ => {});
			},
			addSubmit: function () {
				this.$refs.addForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认添加商品分类吗？', '提示', {}).then(() => {
							this.loading = true;
							var Token = sessionStorage.getItem('Token');
							var timestamp = new Date().getTime();
							var imgUrl = this.imgUrl;//缩略图
							var para ={ token: Token, timestamp:timestamp, img:this.imgUrl, categoryName:this.addForm.categoryName,categoryStatus:this.addForm.categoryStatus, sortNo:this.addForm.sortNo};
							addGoodsCategory(para).then((res) => {
								var res = res.data;
								if (res.code != 0) {
									this.$message({
										message: res.message,
										type: 'error'
									});
								}else{
									this.loading = false;
									this.$message({
										message: '添加商品分类成功',
										type: 'success'
									});
									this.$refs['addForm'].resetFields();
									this.$refs.upload1.clearFiles();
									this.dialogVisible = false;
									this.getCategory();
								}
							});
						});
					}
				});
			},
			// 修改商品分类
			handleEdit(index, row) {
				this.editFormVisible = true;
				this.editForm = Object.assign({}, row);
			},
			//编辑
			editSubmit: function () {
				this.$refs.editForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交编辑的商品分类吗？', '提示', {}).then(() => {
							this.loading = true;
							var Token = sessionStorage.getItem('Token');
							var timestamp=new Date().getTime();
							var para = Object.assign({token: Token, timestamp:timestamp}, this.editForm);
							editGoodsCategory(para).then((res) => {
								var res = res.data;
								if (res.code != 0) {
									this.$message({
										message: res.message,
										type: 'error'
									});
								}else{
									this.loading = false;
									this.$message({
										message: '修改商品分类成功',
										type: 'success'
									});
									this.$refs['editForm'].resetFields();
									this.editFormVisible = false;
									this.getCategory();
								}
							});
						});
					}
				});
			},
			//单个/批量删除
			batchRemove: function () {
				var ids = this.sels.map(item => item.id).toString();
				this.$confirm('确认删除选中的商品分类吗？', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					var Token = sessionStorage.getItem('Token');
					var timestamp=new Date().getTime();
					let para = {token: Token, timestamp:timestamp, ids: ids };
					delGoodsCategory(para).then((res) => {
						var res = res.data;
						if (res.code != 0) {
							this.$message({
								message: res.message,
								type: 'error'
							});
						} else{
							this.listLoading = false;
							this.$message({
								message: '删除商品分类成功',
								type: 'success'
							});
							this.getCategory();
						}
						
					});
				}).catch(() => {
			
				});
			},
			//获取用户列表
			getCategory: function() {
				var Token = sessionStorage.getItem('Token');
				var timestamp=new Date().getTime();
				let para = {
					token: Token,
					curpage:this.page,
					pagesize:this.size,
					timestamp: timestamp
				};
				this.loading = true;
				getGoodsCategory(para).then((res) => {
					var res = res.data;
					if (res.code != 0) {
						this.$message({
							message: res.message,
							type: 'error'
						});
					} else {
						this.total = res.data.total;
						this.List = res.data.list;
						this.loading = false;
					}
				});
			},
		},
		beforeDestroy() {
		  clearInterval(this.timer);        
		  this.timer = null;
		}
	};
</script>

<style scoped>
.el-button.is-disabled{
	color: #bfcbd9;
	    cursor: not-allowed;
	    background-image: none;
	    background-color: #eef1f6;
	    border-color: #d1dbe5;
}
.el-pagination {
    white-space: nowrap;
    padding: 2px 5px;
    color: #48576a;
}
.text{
	font-family: "arial, helvetica, sans-serif";
	font-size: 16px;
	color: #333;
	margin-top: 8px;
	margin-left: 10px;
	font-weight: 600;
}
</style>
